package com.zklh.leetcode.tree.middle;

import com.zklh.leetcode.tree.model.TreeNode;

/**
 * @author 坐看落花
 * @version 1.0
 * @date 2021/5/24 20:45
 */
public class BinaryTreeLeftFinder {


    private int result = 0;
    private boolean finded = false;

    public int find(TreeNode root) {
        int deep = deep(root);
        find(root, 1, deep);
        return result;
    }


    public void find(TreeNode root,int d, int deep) {

        if (root == null || finded) {
            return;
        }
        if (d == deep) {
            result = root.val;
            finded = true;
            return;
        }
        find(root.left,d+1, deep);
        find(root.right,d+1, deep);
    }


    private int deep(TreeNode root) {

        if (root == null) {
            return 0;
        }
        return 1 +Math.max(deep(root.left), deep(root.right));
    }


    public static void main(String[] args) {

        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.left = new TreeNode(4);
        root.right.left = new TreeNode(5);
        root.right.right = new TreeNode(6);
        root.right.left.left = new TreeNode(7);

        BinaryTreeLeftFinder finder = new BinaryTreeLeftFinder();
        System.out.println(finder.find(root));

    }

}
